package com.xyhuo.leetcode.january;

import java.util.*;

/**
 * 最小栈
 * https://leetcode.cn/problems/min-stack/
 *
 * @time 2024/1/22
 */
public class MinStack {

//    public static void main(String[] args) {
//        MinStack ms = new MinStack();
//        ms.push(512);
//        ms.push(-1024);
//        ms.push(-1024);
//        ms.push(512);
//        ms.pop();
//        int min = ms.getMin();
//        ms.pop();
//        int min1 = ms.getMin();
//        ms.pop();
//        int min2 = ms.getMin();
//    }

    Deque<Integer> deque;
    Queue<Integer> queue;

    public MinStack() {
        deque = new ArrayDeque<>();
        queue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });
    }

    public void push(int val) {
        deque.addLast(val);
        queue.add(val);
    }

    public void pop() {
        Integer removeVal = deque.removeLast();
        Iterator<Integer> iterator = queue.iterator();
        while (iterator.hasNext()) {
            if (iterator.next().equals(removeVal)) {
                iterator.remove();
                break;
            }
        }
    }

    public int top() {
        return deque.getLast();
    }

    public int getMin() {
        return queue.peek();
    }
}
